package com.ruoyi.web.controller.warning;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.domain.warning.FactorInfo;
import com.ruoyi.web.domain.warning.WarningConfig;
import com.ruoyi.web.enums.FactorTypeEnum;
import com.ruoyi.web.pojo.request.warning.WarningConfigRequest;
import com.ruoyi.web.service.warning.FactorInfoService;
import com.ruoyi.web.service.warning.WarningConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "传感器预警",tags = "传感器预警")
@RestController
@RequestMapping("/factor-warning")
public class FactorWarningController extends BaseController {

    @Autowired
    private FactorInfoService factorInfoService;

    @Autowired
    private WarningConfigService warningConfigService;

    @ApiOperation("分类查询因子")
    @GetMapping("/getFactorByType")
    public R<List<FactorInfo>> getFactorByType(String type)
    {
        List<FactorInfo> factorInfos = factorInfoService.getByType(type);
        return R.ok(factorInfos);
    }

    @GetMapping("/getFactorType")
    @ApiOperation("得到因子类型")
    public R<Map> getFactorType() {
        return R.ok(FactorTypeEnum.getDescMap());
    }


    @PostMapping("/addOrUpdateWarningConfig")
    @ApiOperation("新增或修改预警配置")
    public R<Map> addOrUpdateWarningConfig(@RequestBody WarningConfig config) {
        warningConfigService.saveOrUpdateInfo(config);
        return R.ok();
    }

    @PostMapping("/updateEnable")
    @ApiImplicitParams({  @ApiImplicitParam(name = "configId",value = "预警配置id", required = true) ,
            @ApiImplicitParam(name = "flag",value = "0 启用 1 停用", required = true)})
    @ApiOperation("启用或停用预警配置")
    public R<Map> updateEnable(@RequestParam Integer configId,@RequestParam Integer flag) {
        warningConfigService.updateEnable(configId,flag);
        return R.ok();
    }

    @ApiOperation("分页获取预警配置")
    @GetMapping("/queryPage")
    public TableDataInfo<WarningConfig> queryPage(WarningConfigRequest request){
        startPage();
        List<WarningConfig> configs = warningConfigService.queryPage(request);
        return getDataTable(configs);
    }

}
